package com.example.json;

import java.beans.Transient;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * @ClassName: User
 * @Description: 用户实体类
 * @author liuwj3
 * @date 2018年10月15日 下午8:04:29
 *
 */
public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	// 序列化时属性重命名
	@JsonProperty(value = "nickName")
	private String name;
	private int age;
	private String love;
	@JsonIgnore
	private String appearance;
	
	// 用于指示和处理用匹配属性对表示的父/子关系的一对注释，与@JsonBackReference配对使用，不影响序列化，暂时不知道干什么用
	@JsonManagedReference
	private List<User> child;
	
	// 序列化时忽略该属性
	@JsonBackReference
	private User parent;

	/**
	 * 得到内部类实例
	 * 
	 * @param name
	 * @param age
	 * @return
	 */
	public static Builder getBuilder(String name, int age) {
		return new Builder(name, age);
	}

	public void addChild(User u) {
		if (child == null) {
			child = new ArrayList<>();
		}
		child.add(u);
	}

	public User() {
	}

	public User(Builder builder) {
		name = builder.name;
		age = builder.age;
		love = builder.love;
		appearance = builder.appearance;
		child = builder.child;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Transient
	public String getLove() {
		return love;
	}

	public void setLove(String love) {
		this.love = love;
	}

	public String getAppearance() {
		return appearance;
	}

	public void setAppearance(String appearance) {
		this.appearance = appearance;
	}

	public List<User> getChild() {
		return child;
	}

	public void setChild(List<User> child) {
		this.child = child;
	}

	public User getParent() {
		return parent;
	}

	public void setParent(User parent) {
		this.parent = parent;
	}

	public static class Builder {
		private final String name;
		private final int age;
		private String love;
		private String appearance;
		private List<User> child;

		public Builder(String name, int age) {
			this.name = name;
			this.age = age;
		}

		public Builder love(String love) {
			this.love = love;
			return this;
		}

		public Builder appearance(String appearance) {
			this.appearance = appearance;
			return this;
		}

		public Builder child(List<User> child) {
			this.child = child;
			return this;
		}

		public User build() {
			return new User(this);
		}
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", love=" + love + ", appearance=" + appearance + ", child="
				+ child + ", parent=" + parent + "]";
	}

}
